package com.itic.appbase.applications.sys.syssetting.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.applications.sys.syssetting.persistence.dao.SysSettingDao;
import com.itic.appbase.applications.sys.syssetting.persistence.model.SysSetting;
import com.itic.appbase.applications.sys.syssetting.persistence.model.SysSettingExample;


@Service
@Transactional
public class SysSettingService
     extends CrudService<SysSettingDao,SysSetting,SysSettingExample> {
    @Autowired
    private SysSettingDao sysSettingDao;

	public String getByKey(String key) {
		return sysSettingDao.getByKey(key);
	}

	/**
	 * 根据更新VALUE值
	 * @param key
	 * @param value
	 * @return
	 */
	public String editByKey(String key, String value) {
		String result = FAILED;
		SysSettingExample example = new SysSettingExample();
		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andSysKeyEqualTo(key);
		List<SysSetting> list = sysSettingDao.selectByExample(example);
		if( list.size() > 0 ){
			SysSetting sysSetting = list.get(0);
			sysSetting.setSysValue(value);
			sysSettingDao.updateByPrimaryKeySelective(sysSetting);
			result = SUCCESS;
		}
		return result;
	}

	/**
	 * 编辑三级分销的配置信息
	 * @param onePeople
	 * @param onePercent
	 * @param twoPeople
	 * @param twoPercent
	 * @return
	 */
	public String editThreeset(String onePeople, String onePercent,
			String twoPeople, String twoPercent) {
		this.editByKey("MARKET_SET_ONE_PEOPLE",onePeople);
		this.editByKey("MARKET_SET_ONE_PERCENT",onePercent);
		this.editByKey("MARKET_SET_TWO_PEOPLE",twoPeople);
		this.editByKey("MARKET_SET_TWO_PERCENT",twoPercent);
		return SUCCESS;
	}


}